package com.example.testapp.activity.ui;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.widget.FrameLayout;

import com.example.testapp.R;
import com.example.testapp.activity.view.pager.TestViewPager;

/**
 * Created by l on 2016/9/6.
 */
public class OutlineContainer extends FrameLayout implements Animatable {

    private Paint  mOutlinePaint;

    private boolean mIsRunning = false;
    private long  mStartTime;
    private float   mAlpha = 1.0f;
    private static final long ANIMATION_DURATION = 500;
    private static final long FRAME_DURATION = 1000 / 60;

    private final Interpolator mInterpolator = new Interpolator() {
        @Override
        public float getInterpolation(float input) {
            input -= 1.0f;
            return input * input * input + 1.0f;
        }
    };

    public OutlineContainer(Context context) {
        super(context);
        init();
    }

    public OutlineContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public OutlineContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mOutlinePaint = new Paint();
        mOutlinePaint.setAntiAlias(true);
        mOutlinePaint.setStrokeWidth(doToPx(getResources(), 2));
        mOutlinePaint.setColor(getResources().getColor(R.color.blue));
        mOutlinePaint.setStyle(Paint.Style.STROKE);

        int padding = doToPx(getResources(), 10);
        setPadding(padding, padding, padding, padding);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        int offset = doToPx(getResources(), 5);
        if(mOutlinePaint.getColor() != TestViewPager.sOutlineColor){
            mOutlinePaint.setColor(TestViewPager.sOutlineColor);
        }
        mOutlinePaint.setAlpha((int)(mAlpha * 255));
        Rect rect = new Rect(offset, offset, getMeasuredWidth() - offset, getMeasuredHeight() - offset);
        canvas.drawRect(rect, mOutlinePaint);
    }

    public void start() {
        if(mIsRunning)
            return;
        mIsRunning = true;
        mStartTime = AnimationUtils.currentAnimationTimeMillis();
        post(mUpdate);
    }

    @Override
    public void stop() {
        if(!mIsRunning)
            return;
        mIsRunning = false;
    }

    private Runnable mUpdate = new Runnable() {
        @Override
        public void run() {
           long now = AnimationUtils.currentAnimationTimeMillis();
           long duration = now - mStartTime;
           if(duration >= ANIMATION_DURATION){
               mAlpha = 0.0f;
               invalidate();
               stop();
               return;
           }else{
               mAlpha = mInterpolator.getInterpolation(1 - duration / (float)ANIMATION_DURATION);
               invalidate();
           }
            postDelayed(mUpdate, FRAME_DURATION);
        }
    };

    @Override
    public boolean isRunning() {
        return mIsRunning;
    }

    public void setOutlineAlpha(float alpha) {
          this.mAlpha = alpha;
    }

    private int doToPx(Resources res, int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.getDisplayMetrics());
    }
}
